#coding:utf8
from help import * 

#config
enable_invert = False
dst_arr = 'aero_pin_xl.cur'
move_task_delay1 = 0.1
move_task_delay2 = 0.18
#微调角度速率配置，可添加
is_working = False
sca_l = [1, 0.3]
max_zi = 60 #最多数字坐标个数
max_l = 3000 #最大数字坐标长度
main_win_size =  (50, 24)  #提示窗口大小，高度是25时，文字显示会上下动。
cir_para = [17, 20, 3, 1, -15, 4]  #鼠标附近提示圆点。

if is_working:
    sca_l = [1, 0.3, 0.1]
    max_zi = 80   
    max_l = 5000    
    main_win_size =  (50, 26)
    cir_para = [18, 23, 3, 1, -15, 4]
    
du_refresh_interval = 0.05
change_win_color = False
mouse_speed_take1 = 0.05
mouse_speed_take2 = 0.1
lock_l = [1, 10, 50] #单步移动模式速度，可添加。第一个固定为1
lock_step_default = 1 #默认移动速度，是上面列表的位置序号
enter_gap = 50
min_d = 25 #数字坐标间最小距离
min_d_mouse = 30 #数字坐标距离鼠标箭头最小距离
icon_file_path = 'aero_ns_xl.cur' #如果运行目录下有此icon文件，会显示系统托盘
sum_max_l = 6 #倍数数字最大长度
draw_txt_yAdd = 10 # 画数字坐标时，文字上下偏移
draw_txt_txtSize = 16  #数字坐标数字大小
draw_txt_pathSize = 6   #数字坐标边缘大小
draw_txt_pathSize2 = 8   #数字坐标边缘大小
draw_cir_out_width = 3 #圆圈外圈宽度
draw_cir_out_rad = 8 #圆圈外圈半径
draw_cir_in_width = 3 #圆圈内圈宽度
draw_cir_in_rad = 7 #圆圈内圈半径
txt_win_size = (60, 30) #数字坐标窗口大小
txt_gap = 10  #数字坐标在窗口中的偏移
cir_win_size = (60, 30) #圆圈窗口大小
cir_gap = 10 #圆圈在窗口中的偏移

main_win_border = 1 # 提示窗口边框大小
main_win_txt_size = 10 #提示窗口文字大小
small_win_trans = True #数字坐标窗口和圆圈窗口是否透明，试验大小时可以改成false
pos_json_path = 'pos.json' #点位坐标文件路径
mouse_delay = 0.3
two_mouse_check_interval = 0.4
to_lap = 0.3

def cal_random(co):
    l = [[1, 1], [1, -1], [-1, 1], [-1, -1]]
    x, y = l[co%4]
    d = 10
    x = d * x
    y = d * y
    a = random.randint(0, 9999)
    t = 2 * math.pi * a / 10000
    dx, dy = math.cos(t), math.sin(t)    
    g = random.randint(10, 20)
    return dx * g, dy * g

def can_jump():
    if cfg.isShow or cfg.show_one_on:
        return False
    return True

def jump_bg():
    co = 0
    while True:
        time.sleep(0.05)
        if time.time() - cfg.left_mouse_time < 0.05 and can_jump() and time.time() - cfg.right_mouse_time < 0.3:
            co += 1
            dx, dy = cal_random(co)            
            mouse.Controller().move(dx, dy)
            
def get_x2_d_sca():
    d = cal_dis ((cfg.snap[-1][1], cfg.snap[-1][2]), (cfg.snap[0][1], cfg.snap[0][2]))
    if d < 400:
        return 2.5
    if d < 1300:
        return 1.5
    if d < 2000:
        return 1
    return 0.7    

screen_config = [[0, 0, 1366, 768],[ -1358, 291, -16, 1029] ]
screen_config = [[0, 0, 1356, 758] ]
def is_in_screen(x, y):    
    for i in screen_config:
        sx, sy, ex, ey = i
        if x >= sx and x <= ex and y >= sy and y <= ey:
            return True
    return False

screen_config_for_indx = [[0, 0, 1366, 768],[ -1366, 291, -1, 1058] ]
def get_screen_indx(x,y):
    for i in range(len(screen_config_for_indx)):
        con = screen_config_for_indx[i]
        if x>=con[0] and y>=con[1] and x<=con[2] and y<= con[3]:
            return i
    return 0
screen_config2 = [[0, 0, 1366, 768],[ -1366, 291, -1, 1058] ]
def is_in_cur_screen(x, y):    
    i =  get_screen_indx(cfg.fix_pos[0], cfg.fix_pos[1])
    sx, sy, ex, ey =   screen_config2[i]    
    if x >= sx and x <= ex and y >= sy and y <= ey:
        return True
    return False

def is_in_all_screen(x, y):
    for i in screen_config2:
        sx, sy, ex, ey = i
        if x >= sx and x <= ex and y >= sy and y <= ey:
            return True
    return False
def get_cur_pos():
    a = mouse.Controller().position
    if type(a) != type((0, 0)):
        return cfg.fix_pos
    if len(a) != 2:
        return cfg.fix_pos
    return a
def is_sca():
    return cfg.sca != 0 or  cfg.sca_du != 0 or  cfg.sca_cur != 0  

def get_near_count(d):
    if d > 150:
        return 1
    if d > 100:
        return 2
    return 3

def get_far_count(d):
    if d < 70:
        return 4
    return 3

def add_idx(st, en, d):
    if st - en > 20:
        xx = int((st+en) / 2)
        return xx - 1, xx + 2
    if st - en > 8:
        xx = int((st+en) / 2)
        return xx, xx + 2
    if st - en >= 4:
        xx = int((st+en) / 2)
        return xx, xx + 1
    return 0, 0

def clear_sca():    
    cfg.sca = 0
    cfg.sca_du = 0
    cfg.sca_cur = 0
    
def lock_step_clear():    
    cfg.lock_step = lock_step_default
    
def clear_lock():    
    cfg.lock = False
    lock_step_clear()
    
def get_cur_pos():
    a = mouse.Controller().position
    if type(a) != type((0, 0)):
        return cfg.fix_pos
    if len(a) != 2:
        return cfg.fix_pos
    return a

def clear(pos, clearLock=True):
    cfg.start_show_time = 0
    cfg.to_time = 0
    cfg.to_change_time = 0
    cfg.showCut = True
    cfg.lock_xing_mode = False
    cfg.lock_jia_mode = False
    cfg.lock_xing_last_move = (0, 0)    
    cfg.w2_last_ge = 0
    cfg.w2_last_to = 0
    cfg.w2_last_dis = 0
    cfg.last_ge = 0
    cfg.w2_min_ge = 0    
    cfg.to = 0
    cfg.force_scroll = False
    cfg.sum = '0'
    clear_sca()
    cfg.force_show = False
    cfg.mouse_mode = 0
    cfg.mouse_m_left_start_time = 0
    cfg.mouse_m_left_dis = [0, 0]
    cfg.mouse_m_right_dis = [0, 0]
    cfg.mouse_m_left_fix_time = 0
    cfg.mouse_m_left_fix_pos = [0, 0]    
    cfg.mouse_m_left_max = [0, 0]
    cfg.clear_time = time.time()
    cfg.fix_scroll = 0        
    cfg.has_invert = False
    cfg.has_pos_vert = False
    cfg.pos_vert_len = 0
    if clearLock:
        clear_lock()
    if pos:
        cfg.fix_pos = get_cur_pos()

def add_and_check(x, y):
    cfg.mouse_m_left_dis[0] += x
    cfg.mouse_m_left_dis[1] += y
    a =  cfg.mouse_m_left_dis[0]
    b =  cfg.mouse_m_left_dis[1]    
    if math.sqrt(a*a+b*b) > 10:
        cfg.mouse_mode = 1
        cfg.mouse_m_left_fix_pos = copy.deepcopy(cfg.mouse_m_left_dis)
        cfg.mouse_m_left_fix_time = time.time()
        
def get_m_du(dx, dy):
    x1, y1 =  cfg.mouse_m_left_fix_pos[0], cfg.mouse_m_left_fix_pos[1]
    d1 = math.atan2(y1, x1)
    d2 = math.atan2(dy, dx)
    print('d1,d2', d1, d2)
    d = d1 - d2
    if d >  math.pi:
        d -= 2 * math.pi
    if d < - math.pi:
        d += 2 * math.pi
    return abs(d)
    
def add_and_check2(x, y):
    if time.time() - cfg.mouse_m_left_fix_time > 0.8:
        print('set -1 p4')
        cfg.mouse_mode = -1
        return
    
    cfg.mouse_m_left_dis[0] += x
    cfg.mouse_m_left_dis[1] += y
    a, b =  cfg.mouse_m_left_max
    c, d =  cfg.mouse_m_left_dis    
    if c *c +d *d > a * a + b * b:
        cfg.mouse_m_left_max = copy.deepcopy(cfg.mouse_m_left_dis)
        if math.sqrt(c *c +d *d) > 250:
            print('set -1 p6')
            cfg.mouse_mode = -1
            return        
    else:
        dx =  cfg.mouse_m_left_max[0] - cfg.mouse_m_left_dis[0] 
        dy =  cfg.mouse_m_left_max[1] - cfg.mouse_m_left_dis[1]
        
        if math.sqrt(dx*dx+dy*dy) > 10:
            
            d = get_m_du(-dx, -dy)
            print('max r', -dx, -dy, d)
            if d > math.pi * 0.5:                
                cfg.mouse_mode = -1
                print('set -1 p5', dx, dy)
                return            
    
def add_and_check_right(x, y):     
    cfg.mouse_m_right_dis[0] += x
    cfg.mouse_m_right_dis[1] += y
    a =  cfg.mouse_m_right_dis[0]
    b =  cfg.mouse_m_right_dis[1]
    if math.sqrt(a*a+b*b) > 10:
        d = get_m_du(a, b)
        if d > math.pi * 0.5:
            cfg.mouse_mode = 2
            cfg.start_show_time = time.time()
            return
        print('set -1 p7')
        cfg.mouse_mode = -1
        return        
            
    
def deal_mouse_mode(h, x, y):
    if time.time() - cfg.clear_time < 0.2:
        return
    if cfg.mouse_mode in (-1, 2):        
        return
    
    if h in cfg.left_mouse:
        cfg.mouse_m_left_last_time = time.time()
        
    if cfg.mouse_mode == 0:
        if h not in cfg.left_mouse:
            print('set -1 p3')
            cfg.mouse_mode = -1
            return
        
        if cfg.mouse_m_left_start_time == 0:
            print('set start', x, y)
            cfg.mouse_m_left_start_time = time.time()
            
        add_and_check(x, y)
        return
    
    if h in cfg.left_mouse:
        add_and_check2(x, y)
        return
    add_and_check_right(x, y)

def check_mouse_mode():
    if cfg.mouse_mode in (-1, 2):        
        return    
    
    if cfg.mouse_mode == 0:
        if cfg.mouse_m_left_start_time == 0:
            return
            
        if time.time() - cfg.mouse_m_left_start_time > 0.5:
            print('set -1 p1')
            cfg.mouse_mode = -1
            return
        return
    
    d =time.time() - cfg.mouse_m_left_fix_time     
    if  d> 2:
        print('set -1 p2', d)
        cfg.mouse_mode = -1
        return            

class Cg():
    def __init__(self) -> None:
        self.has_invert = False
        self.start_show_time = 0        
        self.has_pos_vert = False
        self.pos_vert_len = 0
        
        self.maxi = 0
        self.fix_scroll = 0
        self.clear_time = 0
        self.mouse_mode = 0
        self.mouse_m_left_start_time = 0        
        self.mouse_m_left_dis = [0, 0]
        self.mouse_m_right_dis = [0, 0]
        self.mouse_m_left_fix_time = 0
        self.mouse_m_left_fix_pos = [0, 0]
        self.mouse_m_left_max = [0, 0]
        
        self.x2_mode = False
        self.x2_mode_d = 100
        self.x2_mode_dst = [0, 0]
        
        self.w2_last_dis = 0 # to 不变时，到fix_pos 的距离
        self.w2_last_to = 0
        self.w2_last_ge=0
        self.w2_min_ge=0
        
        self.last_ge = 0  # 上次显示的坐标图标个数,不包括0
        
        self.isOn2 = False #isOn2如果是false，鼠标点击后不开启显示模式；是true，点击后开启显示模式。
        self.to = 0
        self.to_time = 0
        self.mouse_moving_type = []
        self.always_show_one = True
        self.reset_show_one = False
        self.force_show = False
        self.fix_pos = (0, 0)  #起始位置
        self.isShow = False #是否显示
        self.showCut = True
        self.sum = '0' #倍数
        self.move_task = None #sum不为0，移动鼠标，产生移动任务
        self.last_move_time = 0  #上次鼠标移动的时间。检测鼠标移动停顿，再执行移动任务。
        self.click_time = -1 #点击任务
        self.set_fix_time = 0 #移动后，设置起始位置，并设置此时间。用来处理移动出屏幕范围的情况。
        self.is_pressed = False  #左键是否按下
        self.last_mouse_pos = (0, 0)  #键盘操作时鼠标的位置。移动任务需要。
        self.ctrl_pressed = False  # ctrl键按下
        self.alt_pressed= False  #alt键按下
        self.lock = False #数字键盘用来单步移动。
        self.lock_xing_mode = False
        self.lock_jia_mode = False
        self.lock_xing_last_move = (0, 0)
        self.force_scroll = False
        self.left_mouse = []
        self.right_mouse = []
        self.lock_step = lock_step_default #单步移动的速度。
        self.sca = 0  #微调角度的比例
        self.sca_du = 0 #微调角度上次剩余的度数
        self.sca_cur = 0 #微调角度固定点的度数
        self.sca_cur_pos = (0, 0) #微调角度固定点的位置
        self.pause_press_time = 0 #pause键按下的时间，用来判断双击
        self.pause_key = True #pause按下变换此值，用来隐藏窗口。
        self.pause_move = None #双击pause键，产生移动任务
        self.ctrl_l_time = 0 #左ctrl按下时间，用来判断双击。
        self.pause_pressed = False # pause 按住
        self.pause_pressed_then_other = False  #pause按住组合键        
        self.real_mouse_time = 0

        self.mouse_moving_start_time = 0
        self.mouse_moving_start_pos = (0, 0)
        self.mouse_moving_last_is_fast = False
        self.mouse_moving_has_two_mosue = False
        self.last_mouse_moving_has_two_mosue = False
        self.is_suo = False
        self.suo_dis = 0
        self.suo_to = 0
        self.to_change_time = 0
        
        self.x2_pressed = False
        self.x2_pressed_then_other =False
        self.mouse_pressed = False
        self.move_to_win = False
        self.show_one_onetime = False
        self.enter_pressed = False
        self.enter_pressed_then_other = False
        self.cmd_mode =False         
        self.set_mode = False
        self.pos_m = {}
        self.key_input_time = 0
        self.show_one_on = self.always_show_one
        self.show_mouse_tip = True
        self.show_mouse_tip2 = True
        self.w2showing = False
        self.w2snap = []
        self.snap = []
        self.left_mouse_time = 0    
        self.right_mouse_time = 0
        self.w2_to_changing = False
        
cfg = Cg()
def set_to_change_time():
    cfg.to_change_time = time.time()
    cfg.w2_to_changing = True
    
def get_scroll_sca():
    if cfg.to<600:
        return 200
    if cfg.to<1000:
        return 250
    return 300

def get_suo_di():
    if cfg.to<300:
        return 20
    if cfg.to<1000:
        return 25
    return 30

def get_to_sca():
    if cfg.to < 100:
        return 15
    if cfg.to<300:
        return 20
    if cfg.to<1000:
        return 25
    return 30

def mouse_moving_check():
    if len(cfg.mouse_moving_type) < 2:
        return -1
    if cfg.mouse_moving_type[0] < 100 and  cfg.mouse_moving_type[1] < 150:
        return 0
    return 1


